package models

import (
	"gorm.io/gorm"
	"ke-srv/basic/config"
)

type Ke struct {
	gorm.Model
	UserId         int     `gorm:"type:int;comment:'用户id'"`
	Title          string  `gorm:"type:varchar(30);comment:'课程标题'"`
	Comment        string  `gorm:"type:varchar(30);comment:'课程内容'"`
	Num            int     `gorm:"type:int;comment:'课程播放量'"`
	Address        string  `gorm:"type:varchar(30);comment:'视频播放地址'"`
	Price          float64 `gorm:"type:decimal(10,2);comment:'课程价格'"`
	Classification string  `gorm:"type:varchar(30);comment:'视频分类'"`
	Img            string  `gorm:"type:varchar(200);comment:'视频图片'"`
	Status         int     `gorm:"type:int;default:1;comment:'类型: 1没付费 2付费'"`
}

func (k *Ke) TableName() string {
	return "ke"
}

// 添加课程
func (k *Ke) AddKe() error {
	return config.DB.Create(&k).Error
}

// 展示
func (k *Ke) ListKe() (kes []*Ke, err error) {
	err = config.DB.Find(&kes).Error
	return kes, err
}

// 删除
func (k *Ke) DeleteKe() error {
	return config.DB.Where("id = ?", k.ID).Delete(&k).Error
}

// 获取课程id
func (k *Ke) GetKeById(keId int) error {
	return config.DB.Where("id = ?", keId).Limit(1).Find(&k).Error
}

// 课程分类
func (k *Ke) KeList(page, pageSize int, title string) (keList []*Ke, err error) {
	offset := (page - 1) * pageSize
	db := config.DB.Model(&Ke{})
	if k.Title != "" {
		db = db.Where("title like ?", "%"+title+"%")
	}
	if page > 0 || pageSize > 0 {
		db = db.Limit(page).Offset(offset)
	}
	db.Find(&keList)
	return keList, err
}

// 课程详情
func (k *Ke) DetailKe(keId int) error {
	return config.DB.Where("id = ?", keId).Limit(1).Find(&k).Error
}

// 用户收藏
func (k *Ke) CollectUser(keId int) error {
	return config.DB.Where("id = ?", keId).Limit(1).Find(&k).Error
}
